package com.bidevalution.controller.task;

import java.util.Date;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.bidevalution.business.service.BeRoomDeviceService;
import com.bidevalution.business.service.BeRoomService;
import com.bidevalution.business.service.BeSectionInfoService;
import com.bidevalution.business.service.BeSectionProgressService;
import com.bidevalution.controller.util.LiveSdkUtil;


import com.bidevalution.dao.entity.BeRoom;
import com.bidevalution.dao.entity.BeRoomDevice;
import com.bidevalution.dao.entity.BeSectionInfo;
import com.bidevalution.dao.entity.BeSectionProgress;
import com.bidevalution.support.context.SectionStateEnum;
import com.bidevalution.support.context.TencentApiConfig;
import com.qtp.core.util.DateUtil;
/**
 * 标段房间直播流管理
 * 
 * @author 戴志锋
 *
 */
//@Component
public class LiveTask {
	@Autowired
	private BeSectionProgressService beSectionProgressService;
	@Autowired
	private BeSectionInfoService beSectionInfoService;
	@Autowired
	private BeRoomService beRoomService;
	@Autowired
	private BeRoomDeviceService beRoomDeviceService;
	private Logger logger = Logger.getLogger(getClass());
	@Value("${section.live.opentime}")
	private String opentime;
	@Value("${section.live.closetime}")
	private String closetime;

	/*
	 * 定时查询结束的标段，如果标段结束时间大于当前时间closetime的数量，则断掉该标段所在房间的直播流
	 * 如果该房间还有其他标段在直播，则判断该标段的直播是否开启（根据opentime来确定） 如果当前时间大于opentime则直播未开启。则断掉直播流
	 */
	//@Scheduled(cron = "0 */1 *  * * * ")
	public void dropStreamTask() {
		
		BeSectionInfo beSectionInfo;
		BeRoom beRoom;
		label: for (BeSectionProgress beSectionProgress : beSectionProgressService
				.selectLiveList(Integer.valueOf(closetime))) {
			beSectionInfo = beSectionInfoService.getBeSectionInfoById(beSectionProgress.getSectionId());
			if (beSectionInfo != null) {
				beRoom = beRoomService.selectByName(beSectionInfo.getBidOpenAddress());
				if (beRoom != null) {
					// 判断是否还有其他同一个房间有其他标段正在使用视频直播的情况
					for (BeSectionInfo beSectionInfo1 : beSectionInfoService
							.getLiveListByRoom(beSectionInfo.getBidOpenAddress())) {
						if (!SectionStateEnum.finished.getValue().equals(beSectionInfo1.getSectionStage())
								&& (DateUtil.secondNum(new Date(), beSectionInfo1.getBidOpenTime()) / 60) <= Integer
										.valueOf(opentime)) {
							continue label;
						}
					}
					for (BeRoomDevice beRoomDevice : beRoomDeviceService.selectByRoomId(beRoom.getId())) {
						logger.info("断开房间的视频流:" + beRoom.getName());
						LiveSdkUtil.dropStream(TencentApiConfig.tencentApiSecretId,TencentApiConfig.tencentApiSecretKey,beRoomDevice.getPushDomainName(), beRoomDevice.getAppName(),
								beRoomDevice.getStreamName());
					}
				}
			}
		}
	}

	/*
	 * 由于视频流在项目没有开启的时候是断流的，则需要恢复流 开标时间三十分钟前恢复直播流
	 */
	//@Scheduled(cron = "0 */1 * * * * ")
	public void resumeStreamTask() {
		BeRoom beRoom;
		for (BeSectionInfo beSectionInfo : beSectionInfoService.getLiveBeSectionList()) {
			beRoom = beRoomService.selectByName(beSectionInfo.getBidOpenAddress());
			// 如果开标时间还有三十分钟就可以启动恢复直播流
			if (beRoom != null && (DateUtil.secondNum(new Date(), beSectionInfo.getBidOpenTime()) / 60) <= Integer
					.valueOf(opentime)) {
				for (BeRoomDevice beRoomDevice : beRoomDeviceService.selectByRoomId(beRoom.getId())) {
					// 恢复直播流
					LiveSdkUtil.resumeStream(TencentApiConfig.tencentApiSecretId,TencentApiConfig.tencentApiSecretKey,beRoomDevice.getPushDomainName(), beRoomDevice.getAppName(),
							beRoomDevice.getStreamName());
				}
			}
		}
	}

	
}
